import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/utils/store.js';

//注册全局组件  router-link   router-view
Vue.use(VueRouter)

// 1. 定义 (路由) 组件。
// 可以从其他文件 import 进来
// @--src

// 2. 定义路由
const routes = [{
		name: 'Home',
		path: '/home',
		redirect: '/'
	},
	{
		name: 'Home',
		path: '/',
		component: () => import('@/views/Home.vue'),
		children: [
			{
				path: '/checkin',
				component:  () => import('@/views/customer_manage/checkin/index.vue'),
			},
			{
				path: '/customer',
				component:  () => import('@/views/customer_manage/customer/index.vue'),
			},
			{
				path: '/outgoing',
				component:  () => import('@/views/customer_manage/outgoing/index.vue'),
			},
			{
				path: '/retreat',
				component:  () => import('@/views/customer_manage/retreat/index.vue'),
			},
			{
				path: '/customerFood',
				component:  () => import('@/views/food_manage/customerFood/index.vue'),
			},
			{
				path: '/food',
				component:  () => import('@/views/food_manage/food/index.vue'),
			},{
				path: '/manager',
				component:  () => import('@/views/health_steward/manager/index.vue'),
			},{
				path: '/objservice',
				component:  () => import('@/views/health_steward/objservice/index.vue'),
			},
			{
				path: '/nurseContent',
				component:  () => import('@/views/nurse_manage/nurseContent/index.vue'),
			},
			{
				path: '/nurseLevel',
				component:  () => import('@/views/nurse_manage/nurseLevel/index.vue'),
			},{
				path: '/nurseRecording',
				component:  () => import('@/views/nurse_manage/nurseRecording/index.vue'),
			},
			{
				path: '/bed',
				component:  () => import('@/views/room_manage/bed/index.vue'),
			},
			{
				path: '/rooms',
				component:  () => import('@/views/room_manage/rooms/index.vue'),
			},
			{
				path: '/constantType',
				component:  () => import('@/views/constant/constantType/index.vue'),
			},
			{
				path: '/constantsItem',
				component:  () => import('@/views/constant/constantsItem/index.vue'),
			},
			{
				path: '/users',
				component:  () => import('@/views/system_manage/users/index.vue'),
			},
			{
				path: '/role',
				component:  () => import('@/views/system_manage/role/index.vue'),
			},{
				path: '/menu',
				component:  () => import('@/views/system_manage/menu/index.vue'),
			},
			{
				path: '/role_grant',
				component:  () => import('@/views/system_manage/role/grant.vue'),
			},
			{
				path: '/users_grant',
				component:  () => import('@/views/system_manage/users/grant'),
			},

		]
	},
	{
		name: 'Login',
		path: '/login',
		component: () => import('@/views/Login.vue')
	},
	{
		name: 'Register',
		path: '/register',
		component: () => import('@/views/Register.vue')
	},
	{
		path: '*',
		component: () => import('@/views/NotFound.vue')
	},
]

const router = new VueRouter({
	mode: 'hash',
	routes // (缩写) 相当于 routes: routes
})

let  whitelist = [
	'/login','/register',
]


//全局的
router.beforeEach((to, from, next) => {
	let toPath = to.path;
	// if (toPath === '/login'|| whitelist.indexOf(toPath) == -1) {
	if (toPath === '/login'|| toPath === '/register') {
		next()
	} else {   //     /
		//判断是否存在登录状态
		const token = store.state.token;
		if (token) {
			//可以放行
			next();
		} else {
			//没有登录去登录
			next("/login");
		}
	}

})


export default router